package com.bianmaba.beans.datacollection;

import com.bianmaba.spring.data.jpa.domain.annotations.QueryCacheable;
import com.bianmaba.spring.data.jpa.domain.entity.UuidIdentityEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.util.Calendar;
import java.util.Date;


@Entity
@DynamicUpdate()
@DynamicInsert()
//此注解重新定义为是否开始查询缓存
@QueryCacheable
//此注解表示此类支持二级缓存，缓存名称为：defaultEntityCache
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Data
@NoArgsConstructor
public class ExecuteLog extends UuidIdentityEntity {
    @Column(length = 512)
    private String log;
    @Column()
    private Date datetime;

    @ManyToOne
    @JoinColumn(name = "task_instance_id")
    private TaskInstance taskInstance;

    public ExecuteLog(String log, Date datetime, TaskInstance taskInstance) {
        this.log = log;
        this.datetime = datetime;
        this.taskInstance = taskInstance;
    }

    public static ExecuteLog of(String taskInstanceId, String log) {
        return of(TaskInstance.of(taskInstanceId), log);
    }

    public static ExecuteLog of(String taskInstanceId, String log, Date datetime) {
        return of(TaskInstance.of(taskInstanceId), log, datetime);
    }

    public static ExecuteLog of(TaskInstance taskInstance, String log) {
        return of(taskInstance, log, Calendar.getInstance().getTime());
    }

    public static ExecuteLog of(TaskInstance taskInstance, String log, Date datetime) {
        return new ExecuteLog(log, datetime, taskInstance);
    }
}
